Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prosemirror-state

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prosemirror-state

ProseMirror editor state

  • 0.18.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5M
decreased by-9.26%
Maintainers
1
Weekly downloads
 
Created

What is prosemirror-state?

The prosemirror-state package is a part of the ProseMirror toolkit, which is used to manage the state of a ProseMirror editor. It provides a way to represent the editor's state, including the document, selection, and any other metadata. This package is essential for creating and manipulating the state of a ProseMirror editor, allowing for complex text editing functionalities.

What are prosemirror-state's main functionalities?

EditorState

The EditorState class is used to create and manage the state of the editor. This includes the document, selection, and any other metadata. The code sample demonstrates how to create an EditorState instance using a schema and a document parsed from the DOM.

const { EditorState } = require('prosemirror-state');
const { Schema, DOMParser } = require('prosemirror-model');

const schema = new Schema({
  nodes: {
    doc: { content: 'block+' },
    paragraph: { content: 'text*', toDOM: () => ['p', 0] },
    text: { inline: true }
  }
});

const doc = DOMParser.fromSchema(schema).parse(document.querySelector('#content'));
const state = EditorState.create({ doc });
console.log(state);

Transaction

Transactions are used to apply changes to the editor state. The code sample demonstrates how to create a transaction that inserts text into the document and then apply that transaction to the state.

const { EditorState, Transaction } = require('prosemirror-state');
const { Schema, DOMParser } = require('prosemirror-model');

const schema = new Schema({
  nodes: {
    doc: { content: 'block+' },
    paragraph: { content: 'text*', toDOM: () => ['p', 0] },
    text: { inline: true }
  }
});

const doc = DOMParser.fromSchema(schema).parse(document.querySelector('#content'));
let state = EditorState.create({ doc });

let tr = state.tr.insertText('Hello, world!', 1, 1);
state = state.apply(tr);
console.log(state.doc.content);

Plugin

Plugins allow you to extend the functionality of the editor. The code sample demonstrates how to create a plugin that logs keydown events and add it to the editor state.

const { EditorState, Plugin } = require('prosemirror-state');
const { Schema, DOMParser } = require('prosemirror-model');

const schema = new Schema({
  nodes: {
    doc: { content: 'block+' },
    paragraph: { content: 'text*', toDOM: () => ['p', 0] },
    text: { inline: true }
  }
});

const doc = DOMParser.fromSchema(schema).parse(document.querySelector('#content'));

const myPlugin = new Plugin({
  props: {
    handleDOMEvents: {
      keydown(view, event) {
        console.log('A key was pressed:', event.key);
        return false;
      }
    }
  }
});

const state = EditorState.create({ doc, plugins: [myPlugin] });
console.log(state);

Other packages similar to prosemirror-state

FAQs

Package last updated on 24 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc